/**
 * Created by yangzhi on 2019/7/17.
 * desc 编辑器中的自定义上传图片和视频
 */


function setImageVideo(ele,id) {
    layui.use('jquery',function() {
        var $ = layui.$;
        $("#" + ele).next().find('div.layui-layedit-tool').append('<span class="layedit-tool-mid"></span>');
        $("#" + ele).next().find('div.layui-layedit-tool').append('<i class="layui-icon layui-icon-picture" title="插入图片" style="font-size: 18px!important;"  onclick="insertImage(' + id + ')"></i> ');
        $("#" + ele).next().find('div.layui-layedit-tool').append('<i class="layui-icon layui-icon-video" title="插入视频" style="font-size: 18px!important;"   onclick="insertVideo(' + id + ')"></i> ');
    });
}

function  insertImage(indexNumber) {
    layui.use(['jquery','layer'],function() {
        var $ = layui.$;
        var layer=layui.layer;

        layer.open({
            type: 2
            ,title: '插入图片'
            ,content: '/file/goodSelectImage'
            ,area: ['800px', '600px']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#addImageIdsSubmit");

                //监听提交
                iframeWindow.layui.form.on('submit(addImageIdsSubmit)', function(data){
                    var field = data.field; //获取提交的字段

                    var imageUrls=field.imageUrls;
                    var imageIds=field.imageIds;
                    var allImageIds= $("#introImageIdList").val()+imageIds;

                    $("#introImageIdList").val(allImageIds);
                    if(imageUrls!=""){
                        var imageUrlsArray= imageUrls.toString().split(",");
                        var html="";
                        for(var i=0;i<imageUrlsArray.length;i++){
                            if(imageUrlsArray[i]!="") {
                                html = html + "<img src='" + imageUrlsArray[i] + "'><br/>";
                            }
                        }
                        //追加html元素
                        $($("#LAY_layedit_" + Number(indexNumber))).contents().find("body").append(html);
                    }
                    layer.close(index); //关闭弹层
                });
                submit.trigger('click');
            }
        });
    });
}

function  insertVideo(indexNumber) {

    layui.use(['jquery','layer'],function() {
        var $ = layui.$;
        var layer=layui.layer;

        layer.open({
            type: 2
            ,title: '插入视频'
            ,content: '/file/goodSelectVideo'
            ,area: ['800px', '600px']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#addVideoIdsSubmit");

                //监听提交
                iframeWindow.layui.form.on('submit(addVideoIdsSubmit)', function(data){
                    var field = data.field; //获取提交的字段

                    var videoUrls=field.videoUrls;
                    var videoIds=field.videoIds;
                    var allVideoIds= $("#introVideoIdList").val()+videoIds;

                    $("#introVideoIdList").val(allVideoIds);
                    if(videoUrls!=""){
                        var videoUrlsArray= videoUrls.toString().split(",");
                        var html="";
                        for(var i=0;i<videoUrlsArray.length;i++){
                            if(videoUrlsArray[i]!="") {
                                html = html + "<video src='" + videoUrlsArray[i] + "'></video><br/>";
                            }
                        }
                        //追加html元素
                        $($("#LAY_layedit_" + Number(indexNumber))).contents().find("body").append(html);
                    }
                    layer.close(index); //关闭弹层
                });
                submit.trigger('click');
            }
        });
    });
}